package com.inepex.inecharttest.client.inegraphics;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.inepex.inegraphics.impl.client.DrawingAreaGWT;
import com.inepex.inegraphics.shared.Context;
import com.inepex.inegraphics.shared.gobjects.Circle;
import com.inepex.inegraphics.shared.gobjects.GraphicalObject;
import com.inepex.inegraphics.shared.gobjects.Path;
import com.inepex.inegraphics.shared.gobjects.Rectangle;

public class GODrawingTimesTest extends Composite {

	DrawingAreaGWT canvas;
	FlowPanel outputPanel;
	CheckBox drawModeCheckBox;
	TextBox quantity;

	public GODrawingTimesTest() {
		canvas = new DrawingAreaGWT(500, 500);
		outputPanel = new FlowPanel();
		HorizontalPanel hp = new HorizontalPanel();
		HorizontalPanel btnCntnr = new HorizontalPanel();
		VerticalPanel vp = new VerticalPanel();
		drawModeCheckBox = new CheckBox("single GO per update()");
		btnCntnr.add(drawModeCheckBox);
		quantity = new TextBox();
		btnCntnr.add(quantity);
		btnCntnr.add(new Button("circle", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				circle();
			}
		}));
		btnCntnr.add(new Button("pathelement", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				pathElement();
			}
		}));
		btnCntnr.add(new Button("rectangle", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				rectangle();
			}
		}));
		vp.add(btnCntnr);
		vp.add(canvas.getWidget());
		hp.add(vp);
		hp.add(outputPanel);
		initWidget(hp);
	}

	private void circle(){
		com.inepex.inegraphics.shared.gobjects.Circle c = new Circle(250, 250, 200, true, true, getTestContext(), 0);
		drawGo(c);
	}

	private void pathElement(){
		Path p = new Path(0, 0, true, true, 0, getTestContext());
		p.lineTo(500, 500, false);
		drawGo(p);
	}

	private void rectangle(){
		Rectangle r = new Rectangle(10, 10, 470, 470, 0, true, true, getTestContext(), 0);
		drawGo(r);
	}

	private void ellipticArc(){

	}

	private void drawGo(GraphicalObject go){
		int quan = Integer.parseInt(quantity.getText());
		boolean sequence = drawModeCheckBox.getValue();
		long start = System.currentTimeMillis();
		canvas.removeAllGraphicalObjects();
		if(sequence){
			canvas.add(go);
			for (int i = 0; i < quan; i++){
				canvas.update();
			}
		}
		else{
			for (int i = 0; i < quan; i++){
				canvas.add(go);
			}
			canvas.update();
		}
		outputPanel.insert(new Label(((System.currentTimeMillis() - start) / quan) + "ms avg, seq:" + sequence + "  --  " + go.toString()), 0);
	}

	Context getTestContext(){
		return new Context(0.8, "black", 5, "red");
	}
}
